package com.hcms.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.hcms.admin.service.ICmsArticleService;
import com.hcms.mybatis.entity.CmsArticle;
import com.hcms.mybatis.mapper.CmsArticleMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 文章模块 服务实现类
 * </p>
 *
 * @author achenxw@126.com
 * @since 2023-03-31
 */
@Service
public class CmsArticleServiceImpl extends ServiceImpl<CmsArticleMapper, CmsArticle> implements ICmsArticleService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private CmsArticleMapper cmsArticleMapper;

    @Override
    public List<CmsArticle> getList(Integer pageNum, Integer pageSize) {
        logger.info("Run getList(), pageNum={}, pageSize={}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize, "sort ASC, id DESC");
        LambdaQueryWrapper<CmsArticle> queryWrapper = new LambdaQueryWrapper<>();
        return cmsArticleMapper.selectList(queryWrapper);
    }

    @Override
    public CmsArticle getView(Long id) {
        logger.info("Run getView(), id={}", id);
        return cmsArticleMapper.selectById(id);
    }

    @Override
    public int createItem(CmsArticle entity) {
        logger.info("Run createItem()");
        entity.setCreateTime(new Date());
        return cmsArticleMapper.insert(entity);
    }

    @Override
    public int updateItem(Long id, CmsArticle entity) {
        logger.info("Run updateItem(), id={}", id);
        entity.setId(id);
        entity.setUpdateTime(new Date());
        return cmsArticleMapper.updateById(entity);
    }

    @Override
    public int deleteItem(String ids) {
        logger.info("Run deleteItem(), ids={}", ids);
        String[] split = ids.split(",");
        List<String> list = Arrays.asList(split);
        List<Long> collect = list.stream().map(Long::parseLong).collect(Collectors.toList());
        return cmsArticleMapper.deleteBatchIds(collect);
    }
}
